//
//  BaseListViewController.m
//  OrderFood
//
//  Created by xiaofang wang on 2018/10/24.
//  Copyright © 2018年 zhiyuan. All rights reserved.
//

#import "BaseListViewController.h"

@interface BaseListViewController ()
{
    UIButton* nomoredatabtn;
}
@end

@implementation BaseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)loadTableViewWithFrame:(CGRect)frame tableViewStyle:(UITableViewStyle)style refreshStyle:(LSRefreshType)refreshtype{
    _mytableview = [[UITableView alloc] initWithFrame:frame style:style];
    [self.view addSubview:_mytableview];
    switch (refreshtype) {
            case LSRefreshTypeDefault:{
                //
                [self loadRefreshViewJustHeader:NO];
                break;
            }
            case LSRefreshTypeJustHeader:{
                //
                [self loadRefreshViewJustHeader:YES];
                break;
            }
            case LSRefreshTypeJustFooter:{
                //
                [self loadRefreshViewJustFooter];
                break;
            }
        default:
            break;
    }
}

#pragma mark - 刷新

- (void)loadRefreshViewJustHeader:(BOOL)justheader{
    //
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestNewData)];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = [UIColor lightGrayColor];
    header.stateLabel.font = [UIFont systemFontOfSize:12];
    
    if (_mytableview) {_mytableview.mj_header = header;}
    
    if (!justheader) {
        _ls_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
        _ls_footer.refreshingTitleHidden = YES;
        _ls_footer.stateLabel.hidden = YES;
        _ls_footer.stateLabel.textColor = [UIColor lightGrayColor];
        _ls_footer.stateLabel.font = [UIFont systemFontOfSize:12];
        [_ls_footer setTitle:@"无更多内容" forState:MJRefreshStateNoMoreData];
        
        if (_mytableview) {_mytableview.mj_footer = _ls_footer;}
        
    }
    
}

- (void)loadRefreshViewJustFooter{
    _ls_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    _ls_footer.refreshingTitleHidden = YES;
    _ls_footer.stateLabel.hidden = YES;
    _ls_footer.stateLabel.textColor = [UIColor lightGrayColor];
    _ls_footer.stateLabel.font = [UIFont systemFontOfSize:12];
    [_ls_footer setTitle:@"无更多内容" forState:MJRefreshStateNoMoreData];
    if (_mytableview) {_mytableview.mj_footer = _ls_footer;}
    
}

- (void)requestNewData{
    _ls_footer.stateLabel.hidden = YES;
    self.mytableview.mj_footer.state = MJRefreshStateIdle;
    
}
- (void)requestMoreData{
    _ls_footer.stateLabel.hidden = NO;
}

- (void)ls_headerBeginRefreshing{
    [_mytableview.mj_header beginRefreshing];
}
- (void)ls_headerEndRefreshing{
    [_mytableview.mj_header endRefreshing];
}

- (void)ls_footerEndRefreshing{
    [_mytableview.mj_footer endRefreshing];
}

- (void)ls_reloadData{
    [_mytableview reloadData];
}

#pragma mark-刷新时调用，无数据的话展示相关文字status

- (void)showPlaceHolderViewWithStatus:(NSString*)status listinfo:(NSArray*)listinfo{
    
    BOOL nodata = listinfo.count < 1;
    if (!nodata) {
        [self dismissNoDataView];
        return;
    }
    [self showNoDataViewWithStatus:status];
}

- (void)showNoDataViewWithStatus:(NSString*)status{
    [self dismissNoDataView];
    nomoredatabtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nomoredatabtn.frame = CGRectMake(0, self.mytableview.tableHeaderView.bounds.size.height+self.mytableview.tableFooterView.bounds.size.height, SCREEN_WIDTH, 100);
    [nomoredatabtn setTitle:status forState:UIControlStateNormal];
    [nomoredatabtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    nomoredatabtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_mytableview addSubview:nomoredatabtn];
    _ls_footer.stateLabel.hidden = YES;
}

- (void)dismissNoDataView{
    if (nomoredatabtn) {
        [nomoredatabtn removeFromSuperview];
        nomoredatabtn = nil;
    }
    
}
#pragma mark-分割线补全
- (void)viewDidLayoutSubviews {
    
    if ([self.mytableview respondsToSelector:@selector(setLayoutMargins:)]) {
        
        self.mytableview.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.mytableview respondsToSelector:@selector(setSeparatorInset:)]) {
        
        self.mytableview.separatorInset = UIEdgeInsetsZero;
    }
}

@end
